package zhan.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhan
 * @date 2019-01-03 11:23
 */
public class ControllerMethodInterceptor implements MethodInterceptor {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
//    public static final Logger logger = Logger.getLogger(CommonDataInterceptor.class);

    private final static ObjectMapper jsonMapper = new ObjectMapper();


    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        String s = jsonMapper.writeValueAsString(invocation.getArguments());
        logger.info("Arguments: {}",s);
        logger.info("Before: interceptor name: {}", invocation.getMethod().getName());

        jsonMapper.writeValueAsString(invocation.getArguments());
        logger.info("Arguments: {}");

        Object result = invocation.proceed();

        logger.info("After: result: {}", jsonMapper.writeValueAsString(result));

        return result;
    }
}
